package com.li.commons.util;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport{

	private static final long serialVersionUID = -3447587418168781597L;
	
	private PageWrapper<Object> pageWrapper;
	
	private String queryCondition;
	
	@Override
	public int doStartTag() throws JspException {
        try {
            JspWriter out = this.pageContext.getOut();
            if(pageWrapper == null) {
                return SKIP_BODY;
            }
            int nextPage = 0;
            int prePage = 0;
            if(pageWrapper.getPageNum()<pageWrapper.getPages()){
            	nextPage = pageWrapper.getPageNum()+1;
            }else{
            	pageWrapper.setPageNum(pageWrapper.getPages());
            	nextPage = pageWrapper.getPages();
            }
            if(pageWrapper.getPageNum()>1){
            	prePage = pageWrapper.getPageNum()-1;
            }else{
            	prePage = 1;
            }
            out.println("<td width=\"25%\" nowrap=\"nowrap\" height=\"29\"><span class=\"STYLE1\">共"+pageWrapper.getTotal()+"条纪录，当前第"+pageWrapper.getPageNum()+"/"+pageWrapper.getPages()+"页，每页"+pageWrapper.getPageSize()+"条纪录</span></td>");
            out.println("<td width=\"75%\" valign=\"top\" class=\"STYLE1\"><div align=\"right\">");
            out.println("<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"352\" height=\"20\">");
            out.println("<tbody><tr>");
            out.println("<td width=\"62\" valign=\"middle\" height=\"22\"><div align=\"right\"><img width=\"37\" height=\"15\" src=\"/images/tab/images/first.gif\" onclick=\"go(1);\" style=\"cursor:pointer;\"></div></td>");
            out.println("<td width=\"50\" valign=\"middle\" height=\"22\"><div align=\"right\"><img width=\"43\" height=\"15\" src=\"/images/tab/images/back.gif\" onclick=\"go("+prePage+")\" style=\"cursor:pointer;\"></div></td>");
            out.println("<td width=\"54\" valign=\"middle\" height=\"22\"><div align=\"right\"><img width=\"43\" height=\"15\" src=\"/images/tab/images/next.gif\" onclick=\"go("+nextPage+")\" style=\"cursor:pointer;\"></div></td>");
            out.println("<td width=\"49\" valign=\"middle\" height=\"22\"><div align=\"right\"><img width=\"37\" height=\"15\" src=\"/images/tab/images/last.gif\" onclick=\"go("+pageWrapper.getPages()+");\" style=\"cursor:pointer;\"></div></td>");
            out.println("<td width=\"59\" valign=\"middle\" height=\"22\"><div align=\"right\">转到第</div></td>");
            out.println("<td width=\"25\" valign=\"middle\" height=\"22\"><span class=\"STYLE7\">");
            out.println("<input type=\"text\" size=\"5\" style=\"height: 10px; width: 25px;\" class=\"STYLE1\" name=\"textfield\"  id=\"goPage\">");
            out.println("</span></td>");
            out.println("<td width=\"23\" valign=\"middle\" height=\"22\">页</td>");
            out.println("<td width=\"30\" valign=\"middle\" height=\"22\"><img width=\"37\" height=\"15\" src=\"/images/tab/images/go.gif\" onclick=\"goPage();\"></td>");
            out.println("</tr>");
			out.println("</tbody>");
			out.println("</table>");
			out.println("</div>");
			out.println("</td>"); 		
            out.println("<td width=\"14\"><img src=\"/images/tab/images/tab_22.gif\" width=\"14\" height=\"29\" /></td>");
            
            out.println("<script type=\"text/javascript\">");
            out.println("var query=\"&\""+queryCondition+";");
            out.println("function go(page){");  
            out.println("window.location=\"?pageNum=\"+page+''+query;");
            out.println("}");
            out.println("function goPage(){");
           	out.println("var regex=/^\\d+$/;");
            out.println("var page = document.getElementById(\"goPage\").value;");
            out.println("if(!regex.test(page)){");
            out.println("alert(\"请输入数字！\");");
            out.println("return;");
            out.println("}");
            out.println("if(page > "+pageWrapper.getPages()+" || page < 1){");
            out.println("alert(\"请输入1~"+pageWrapper.getPages()+"的页数！\");");
            out.println("return;");
            out.println("}");
            out.println("window.location=\"?pageNum=\"+page+''+query;"); 
            out.println("}");
            out.println("</script>");
        } catch(Exception e) {
            throw new JspException(e.getMessage());
        }
        return SKIP_BODY;
    }
	
	@Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;

    }

    @Override
    public void release() {
        super.release();
    }

	public PageWrapper<Object> getPageWrapper() {
		return pageWrapper;
	}

	public void setPageWrapper(PageWrapper<Object> pageWrapper) {
		this.pageWrapper = pageWrapper;
	}

	public String getQueryCondition() {
		return "&"+queryCondition;
	}

	public void setQueryCondition(String queryCondition) {
		this.queryCondition = queryCondition;
	}
}
